<template>
	<jk-dialog
		:dialog-state="showDialog"
		:title="`开台-${ruleForm.machineCode}`"
		width="900px"
		:confirm-btn-loading="confirmBtnLoading"
		:show-content-loading="showLoading"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
		@on-visible-change="onVisibleChange"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="140px" :show-message="false">
			<div class="flex-center">
				<el-tag effect="plain" size="large" class="margin-bottom-10" style="font-weight: 700;font-size: 22px;">
					<span class="el-icon-files item-gap"></span>
					<span>{{ ruleForm.machineCode }} ({{ `${ruleForm.startSpinNumber}~${ruleForm.endSpinNumber}` }})</span>
				</el-tag>
			</div>
			<jk-divider-card>
				<el-row>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="开台时间" prop="startSystemTime">
							<el-date-picker v-model="ruleForm.startSystemTime" clearable type="datetime" style="width: 100%;" value-format="yyyy-MM-dd HH:mm:ss" placeholder="开台时间" @change="onTimeChange" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="开台班次" prop="startScheduleShiftName">
							<jk-input v-model="ruleForm.startScheduleShiftName" readonly placeholder=""></jk-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="品种">
							<span class="read-only-2">{{ ruleForm.productName }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="批号">
							<span class="read-only-2">{{ ruleForm.batchCode }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="排产数量">
							<span class="read-only-2">{{ ruleForm.productionQty }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="纸管颜色">
							<span class="read-only-2">{{ ruleForm.paperTubeList ? ruleForm.paperTubeList.map(x => x.name).join(',') : '' }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="生产通知单">
							<span class="read-only-2">{{ ruleForm.prdNoticeCode }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="区域">
							<span class="read-only-2">{{ ruleForm.workshopName }}</span>
						</el-form-item>
					</el-col>
				</el-row>
			</jk-divider-card>
			<jk-divider-card title="仪表信息" icon="el-icon-odometer" class="margin-top-20">
				<el-row>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="开台产量表数" prop="startOutput">
							<jk-number-input v-model="ruleForm.startOutput" placeholder="开台产量表数" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="当前产量采集表数">
							<el-input v-model="ruleForm.startSystemOutput" disabled placeholder="当前产量采集表数" />
						</el-form-item>
					</el-col>
					<!--<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="上次了机产量表数">
							<el-input v-model="ruleForm.lastEndOutput" disabled placeholder="上次了机产量表数" />
						</el-form-item>
					</el-col>-->
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="开台能耗表数" prop="startElectricEnergy">
							<jk-number-input v-model="ruleForm.startElectricEnergy" placeholder="开台能耗表数" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="当前能耗采集表数">
							<el-input v-model="ruleForm.startSystemElectricEnergy" disabled placeholder="当前能耗采集表数" />
						</el-form-item>
					</el-col>
					<!--<el-col :span="12">
						<el-form-item class="margin-bottom-10" label="上次了机能耗表数">
							<el-input v-model="ruleForm.lastEndElectricEnergy" disabled placeholder="上次了机能耗表数" />
						</el-form-item>
					</el-col>-->
				</el-row>
			</jk-divider-card>
		</el-form>

	</jk-dialog>
</template>

<script>
    import { clearRedundancyKey, formatTime, tipsBar } from '../../../../utils/jk/jk-tool';
    import { mathJsSub } from '../../../../utils/jk/jk-cals';
    import { workforcemanagementScheduleShiftByTime } from '../../../../api/schedule/empReplacePost';
    import {
        productionPrdNoticeMachineChangeGetMachineData
    } from '../../../../api/production/xsOpen';
    import { productionPrdNoticeLtMachineChangeOpening } from '../../../../api/production/ltNotice';
    import { validateNormal } from '../../../../utils/jk/validator';
    export default {
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            selectMachineItem: {
                type: Object,
                default() {
                    return {};
                }

            }
        },
        data() {
            return {
                showLoading: false,
                activeName: '1',
                showDialog: false,
                confirmBtnLoading: false,
                ruleForm: {
                    'endSpinNumber': null,
                    'lastEndElectricEnergy': null,
                    'lastEndOutput': null,
                    'openSpinCount': null,
                    'prdNoticeMachineChangeId': null,
                    'startElectricEnergy': null,
                    'startOutput': null,
                    'startScheduleShiftId': null,
                    'startScheduleShiftName': '',
                    'startSpinNumber': null,
                    'startSystemElectricEnergy': null,
                    'startSystemOutput': null,
                    'startSystemTime': ''
                },
                rules: {
                    startScheduleShiftName: [{ required: true, validator: validateNormal, trigger: 'change' }],
                    openSpinCount: [{ required: true, validator: validateNormal, trigger: 'change' }],
                    startSystemTime: [{ required: true, validator: validateNormal, trigger: 'change' }],
                    startOutput: [{ required: true, validator: validateNormal, trigger: 'change' }],
                    startElectricEnergy: [{ required: true, validator: validateNormal, trigger: 'change' }]
                }
            };
        },
        methods: {
            onJump() {
                this.$toMainRouter({
                    name: 'XsClose',
                    query: {
                        machineCode: this.ruleForm.machineCode
                    }
                });
            },
            onSpinNumChange() {
                if (this.ruleForm.endSpinNumber) {
                    this.ruleForm.startSpinNumber = this.ruleForm.startSpinNumber || 0;
                    this.$set(this.ruleForm, 'openSpinCount', mathJsSub(this.ruleForm.endSpinNumber, this.ruleForm.startSpinNumber));
                }
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.saveRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            saveRequest() {
                this.confirmBtnLoading = true;
                const params = {
                    'endSpinNumber': this.ruleForm.endSpinNumber,
                    'lastEndElectricEnergy': this.ruleForm.lastEndElectricEnergy,
                    'lastEndOutput': this.ruleForm.lastEndOutput,
                    'openSpinCount': this.ruleForm.openSpinCount,
                    'prdNoticeMachineChangeId': this.ruleForm.prdNoticeMachineChangeId,
                    'startElectricEnergy': this.ruleForm.startElectricEnergy,
                    'startOutput': this.ruleForm.startOutput,
                    'startScheduleShiftId': this.ruleForm.startScheduleShiftId,
                    'startSpinNumber': this.ruleForm.startSpinNumber,
                    'startSystemElectricEnergy': this.ruleForm.startSystemElectricEnergy,
                    'startSystemOutput': this.ruleForm.startSystemOutput,
                    'startSystemTime': this.ruleForm.startSystemTime
                };
                productionPrdNoticeLtMachineChangeOpening(params).then(res => {
                    this.confirmBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$emit('on-confirm');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
            },
            onTimeChange() {
                this.$set(this.ruleForm, 'startScheduleShiftId', null);
                this.$set(this.ruleForm, 'startScheduleShiftName', '');
                this.getScheduleShiftForTimeRequest();
                this.getGatherMachineDataRequest();
            },
            getScheduleShiftForTimeRequest() {
                if (this.ruleForm.workshopId && this.ruleForm.startSystemTime) {
                    return workforcemanagementScheduleShiftByTime({
                        startTime: this.ruleForm.startSystemTime,
                        workshopId: this.ruleForm.workshopId
                    }).then(res => {
                        if (res.data.status === 200) {
                            let resData = res.data.res;
                            if (!resData) {
                                tipsBar(this, 'custom', 'warning', '当前时间未排班，请排班后再操作！');
                                resData = { shiftId: null, shiftName: '' };
                            }
                            this.$set(this.ruleForm, 'startScheduleShiftId', resData.shiftId);
                            this.$set(this.ruleForm, 'startScheduleShiftName', resData.shiftName);
                        }
                    });
                }
            },
            // 获取指定机台采集的仪表数据
            getGatherMachineDataRequest() {
                if (this.ruleForm.machineId && this.ruleForm.machineCode) {
                    return productionPrdNoticeMachineChangeGetMachineData({
                        machineId: this.ruleForm.machineId,
                        machineCode: this.ruleForm.machineCode,
                        selectDateTime: this.ruleForm.startSystemTime,
                        spinGroupId: this.selectMachineItem.spinGroupId
                        // startSpinNumber: this.ruleForm.startSpinNumber
                    }).then(res => {
                        if (res.data.status === 200) {
                            // 开台能耗表数，默认等于采集到的实时能耗
                            this.$set(this.ruleForm, 'startElectricEnergy', res.data.res.currentElectricEnergy);
                            this.$set(this.ruleForm, 'startSystemElectricEnergy', res.data.res.currentElectricEnergy);
                            // 开台产量表数，默认等于采集到的实时产量
                            this.$set(this.ruleForm, 'startOutput', res.data.res.currentOutput);
                            this.$set(this.ruleForm, 'startSystemOutput', res.data.res.currentOutput);
                            // 上次的产量和能耗
                            this.$set(this.ruleForm, 'lastEndOutput', res.data.res.lastEndOutput);
                            this.$set(this.ruleForm, 'lastEndElectricEnergy', res.data.res.lastEndElectricEnergy);

                            this.$set(this.ruleForm, 'openSpinCount', res.data.res.spinCount);
                        }
                    });
                }
            },
            async getDependentData() {
                this.showLoading = true;
                await this.getScheduleShiftForTimeRequest();
                await this.getGatherMachineDataRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.ruleForm = clearRedundancyKey(this.selectMachineItem);
                this.$set(this.ruleForm, 'prdNoticeMachineChangeId', this.selectMachineItem.id);
                this.$delete(this.ruleForm, 'id');
                this.$set(this.ruleForm, 'startSystemTime', formatTime(Date.now()));
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
